package com.dabai.qrtools.qrscanner.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.preference.PreferenceManager;

import androidx.annotation.Nullable;

import com.dabai.qrtools.qrscanner.R;
import com.dabai.qrtools.qrscanner.ui.QrCodeScannerActivity;
import com.google.android.gms.common.util.SharedPreferencesUtils;

/**
 * Description : Other
 *
 * @author BAI
 */
public class ScannerUtils {

    public static void playSound(Context context){
        if (PreferenceManager
                .getDefaultSharedPreferences(context).getBoolean("scanSound",false)){
            MediaPlayer mPlayer = MediaPlayer.create(context, R.raw.beep);
            mPlayer.start();
        }
    }
    public static void playVibration(Context context){
        if (PreferenceManager
                .getDefaultSharedPreferences(context).getBoolean("scanVibration",true)){
            int time = 100;
            Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
            // Vibrate for time milliseconds
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                v.vibrate(VibrationEffect.createOneShot(time, VibrationEffect.DEFAULT_AMPLITUDE));
            } else {
                //deprecated in API 26
                v.vibrate(time);
            }
        }
    }



    /**
     * 得到二维码扫描仪结果
     *
     *
     @Override
     protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
     super.onActivityResult(requestCode, resultCode, data);

     if (resultCode == Activity.RESULT_OK) {
     if (requestCode == 301) {
     if (data != null) {
     String content = ScannerUtils.getQrCodeScannerResult(data);

     }
     }
     }

     }

      *
     *
     * @param data 数据
     * @return {@link String}
     */
    public static String getQrCodeScannerResult(Intent data){
        return data.getStringExtra("qrContent");
    }

    /**
     * 启动二维码扫描仪
     *
     * @param activity 活动
     * @param reqCode  申请代码
     */
    public static void startQrCodeScanner(Activity activity, int reqCode){
        // 跳转逻辑
        Intent intent = new Intent(activity,  QrCodeScannerActivity.class);
        activity.startActivityForResult(intent, reqCode);
    }




}
